<template>
  <div class="content">
    <div class="header">
      <el-button type="primary" @click="add">新增</el-button>
      <el-space class="search">
        <el-input
          v-model="name"
          placeholder="请输入关键字"
          style="width: 200px"
        />
        <el-button @click="search">搜索</el-button>
      </el-space>
    </div>
    <el-table :data="tableData" style="width: 100%">
      <el-table-column prop="title" label="标题">
        <template #default="scope">
          <el-popover
            effect="light"
            trigger="hover"
            placement="top"
            width="auto"
          >
            <template #default>
              <div>name: {{ scope.row.title }}</div>
            </template>
            <template #reference>
              <el-tag>{{ scope.row.title }}</el-tag>
            </template>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column prop="blogTotal" label="相关博客总数" />
      <el-table-column label="操作">
        <template #default="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)"
            >编辑</el-button
          >
          <el-button
            size="small"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      layout="sizes, prev, pager, next, jumper"
      :total="total"
      :page-size="pageSize"
      v-model:current-page="currentPage"
      :page-sizes="[5, 10, 15, 20]"
      @size-change="sizeChange"
      @current-change="currentChange"
      class="pagination"
    />
    <el-dialog v-model="dialogFormVisible" title="修改分类">
      <el-form :model="form">
        <el-form-item label="名称" :label-width="140">
          <el-input v-model="form.title" autocomplete="off" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="cancle">取消</el-button>
          <el-button type="primary" @click="confirm">确认</el-button>
        </span>
      </template>
    </el-dialog>

    <el-dialog v-model="addFormVisible" title="新增分类">
      <el-form :model="form">
        <el-form-item label="名称" :label-width="140">
          <el-input v-model="addForm.title" autocomplete="off" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="addCancle">取消</el-button>
          <el-button type="primary" @click="addConfirm">确认</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import {
  getAllCategories,
  updateCategory,
  deleteCategory,
  searchCategory,
  addCategory,
} from "@/api/category";
import { ElMessage } from "element-plus";
export default {
  name: "Category",
  data() {
    return {
      tableData: [],
      total: 10,
      pageSize: 5,
      currentPage: 1,
      dialogFormVisible: false,
      form: {
        id: "",
        userId: "",
        title: "",
        status: 0,
      },
      name: "",
      addFormVisible: false,
      userInfo: JSON.parse(sessionStorage.getItem("userInfo")),
      addForm: {
        userId: JSON.parse(sessionStorage.getItem("userInfo")).id,
        title: "",
      },
    };
  },
  created() {
    // const userInfo = this.$store.getters.getUser;
    this.getData(this.currentPage, this.pageSize, this.userInfo.id);
  },
  mounted() {
    const userInfo = JSON.parse(sessionStorage.getItem("userInfo"));
    console.log(userInfo);
    this.userInfo = userInfo;
    this.avatar = userInfo["avatar"];
  },
  methods: {
    getData(currentPage, size, id) {
      getAllCategories(currentPage, size, id).then((res) => {
        if (res.code == 200) {
          this.tableData = res.data.categoryDtos;
          this.total = res.data.total;
        }
      });
    },
    sizeChange(size) {
      this.getData(this.currentPage, size, this.userInfo.id);
    },
    currentChange(current) {
      this.getData(current, this.pageSize, this.userInfo.id);
    },
    handleEdit(_, row) {
      this.dialogFormVisible = true;
      this.form.id = row.id;
      this.form.userId = row.userId;
      this.form.title = row.title;
      this.form.status = row.status;
    },
    handleDelete(_, row) {
      const id = row.id;
      deleteCategory(id).then((res) => {
        if (res.code == 200) {
          this.getData(this.currentPage, this.pageSize, this.userInfo.id);
          ElMessage.success("删除分类成功");
        } else {
          ElMessage.error(res.msg);
        }
      });
    },
    confirm() {
      updateCategory(this.form).then((res) => {
        if (res.code == 200) {
          this.dialogFormVisible = false;
          // 重新刷新数据
          this.getData(this.currentPage, this.pageSize, this.userInfo.id);
          ElMessage.success("修改分类成功");
        } else {
          ElMessage.error(res.msg);
        }
      });
    },
    cancle() {
      this.dialogFormVisible = false;
      ElMessage.error("取消修改分类");
    },
    search() {
      const userInfo = JSON.parse(sessionStorage.getItem("userInfo"));
      const id = userInfo.id;
      searchCategory(this.currentPage, this.pageSize, this.name, id).then(
        (res) => {
          if (res.code == 200) {
            this.tableData = res.data.categoryDtos;
            this.total = res.data.total;
          }
        }
      );
    },
    add() {
      this.addFormVisible = true;
    },
    addConfirm() {
      if (this.addForm.title == "") {
        ElMessage.error("标题不能为空");
      } else {
        addCategory(this.addForm).then((res) => {
          if (res.code == 200) {
            this.addFormVisible = false;
            // 重新刷新数据
            this.getData(this.currentPage, this.pageSize, this.userInfo.id);
            ElMessage.success("新增分类成功");
            this.addForm.title == "";
          } else {
            ElMessage.error(res.msg);
          }
        });
      }
    },
    addCancle() {
      this.addFormVisible = false;
      ElMessage.error("取消新增分类");
      this.addForm.title == "";
    },
  },
};
</script>

<style lang="less" scoped>
.content {
  margin-top: 10px;
  .header {
    display: flex;
    justify-content: space-around;
    .search {
      display: flex;
      position: relative;
      flex-direction: row;
      justify-content: center;
      button {
        margin-right: 10px;
      }
    }
  }
  .pagination {
    margin-top: 10px;
    display: flex;
    justify-content: flex-end;
    align-content: space-around;
    margin-right: 100px;
  }
}
</style>
